package com.lzy.cloud.order.service.impl;

import com.lzy.cloud.order.constants.CommonConstants;
import com.lzy.cloud.order.dtos.OrderInfoDetailDto;
import com.lzy.cloud.order.dtos.OrderInfoDto;
import com.lzy.cloud.order.remote.TrainApi;
import com.lzy.cloud.order.request.ticket.train.OrderBookingAppRequest;
import com.lzy.cloud.order.request.ticket.train.OrderBookingInfoAppRequest;
import com.lzy.cloud.order.response.ticket.train.TicketTrainResponse;
import com.lzy.cloud.order.service.OrderInfoDetailService;
import com.lzy.cloud.order.service.OrderInfoService;
import com.lzy.cloud.order.service.OrderInfoTicketTrainService;
import com.lzy.framework.core.ensure.Ensure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Description: 火车票订单
 * @Author:   gyl
 * @Date:     2018/7/6 16:11
 */
@Service
public class OrderInfoTicketTrainServiceImpl implements OrderInfoTicketTrainService {

    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private OrderInfoDetailService orderInfoDetailService;
    @Autowired
    private TrainApi trainApi;
    @Autowired
    private SerialNumberServiceImpl serialNumberServiceImpl;

    /**
     * 方法说明：房车销售订单新增
     * @param    request 火车票请求信息
     * @return     订单支付信息
     * @date        2018/7/6 16:30
     */
    @Override
    public TicketTrainResponse create(String token, OrderBookingAppRequest request, Long userId) {
        OrderBookingInfoAppRequest orderBookingInfo = request.getOrderBookingInfo();

        // 订单编号生成
        String orderId = serialNumberServiceImpl.createSerialNumber(CommonConstants.Biz.TICKET_TRAIN, userId + "") + "";

        // 保存订单
        OrderInfoDto orderInfoDto = new OrderInfoDto();
        orderInfoDto.setOrderId(orderId);
        orderInfoDto.setThirdOrderId(orderBookingInfo.getPartnerOrderNo());
        orderInfoDto.setType(Integer.parseInt(CommonConstants.Biz.TICKET_TRAIN));
        orderInfoDto.setMemberId(userId);
        orderInfoDto.setCount(Integer.parseInt(orderBookingInfo.getHeadCounts()));
        orderInfoDto.setPrice(new BigDecimal(orderBookingInfo.getSeatPrice()));
        orderInfoDto.setOrderTotal(new BigDecimal(orderBookingInfo.getOrderAmount()));
        orderInfoDto.setOrderAmount(new BigDecimal(orderBookingInfo.getOrderAmount()));
        orderInfoDto.setCreateUser(userId);
        Ensure.that(orderInfoService.create(orderInfoDto)).isTrue("00010002");

        // 保存订单详情
        OrderInfoDetailDto orderInfoDetailDto = new OrderInfoDetailDto();
        orderInfoDetailDto.setOrderId(orderId);
        orderInfoDetailDto.setGoodsAmount(new BigDecimal(orderBookingInfo.getOrderAmount()));
        orderInfoDetailDto.setCreateUser(userId);
        Ensure.that(orderInfoDetailService.create(orderInfoDetailDto)).isTrue("00080001");

        // 调用新增火车票的api
        orderBookingInfo.setCustomId(orderId);
        TicketTrainResponse ticketTrainResponse = trainApi.bookingOrder(token, request);

        return ticketTrainResponse;
    }
}
